#include "fig_ui_removead.h"
#include "gameflow/ui_popup.h"
#include "fig_vars.h"

#ifndef PRO_VERSION
#include "admanager/admanager.h"
#endif

#include "fig_constants.h"
#include "fighter.h"
#include "gen_game_state.h"
#include "fig_ui_pausemenu.h"
#include "build.h"


#include "util/log.h"

#ifndef PRO_VERSION
#include "inapppurchase/InAppPurchaseManager.h"
#endif

FIGUIRemoveAd&	FIGUIRemoveAd::Instance()
{
	static FIGUIRemoveAd	Inst;
	return Inst;
}

FIGUIRemoveAd::FIGUIRemoveAd()   : gameflow::UIPopup("ui/removead.ui","button_back"),
									m_pConnectBackButton(NULL),
									m_ConnectinBackButtonDealy(0)
{
	SetWidgetFunctor("button_restore", UI_DELEGATE(FIGUIRemoveAd, OnButtonRestore));
    SetWidgetFunctor("button_buy", UI_DELEGATE(FIGUIRemoveAd, OnButtonBuy));

	m_CounterPopupAnimate.SetLimit(0.25f);
	m_CounterPopupAnimate.ForceReady();

	m_CounterBackPopupAnimate.SetLimit(0.1f);
	m_CounterBackPopupAnimate.ForceReady();

	m_pUIBuyFrame = GetWidget("frame");    
	if(m_pUIBuyFrame)
	{
		m_SizeBuyPopup = m_pUIBuyFrame->GetSize();
		m_PosBuyPopup = m_pUIBuyFrame->GetPosition() + (m_SizeBuyPopup * 0.5f);
	}
    

}

void		FIGUIRemoveAd::Tick(float dt)
{
	if(!m_CounterPopupAnimate.IsReady())
	{
		m_CounterPopupAnimate.Tick(dt);
		math::Vec2 Size = m_SizeBuyPopup * 0.8f;
		Size += ( m_SizeBuyPopup * 0.3f * m_CounterPopupAnimate.GetProgress() );
		
		math::Vec2 Pos = m_PosBuyPopup;
		Pos -=	(Size * 0.5f);

		if(m_pUIBuyFrame)
		{
			m_pUIBuyFrame->SetSize(Size);
			m_pUIBuyFrame->SetPosition(Pos);
		}

		if(m_CounterPopupAnimate.IsReady())
		{
			m_CounterBackPopupAnimate.Reset();
			ui::UIWidget* pTempWidget = GetWidget("buybox_buttons");
			if(pTempWidget)
				pTempWidget->SetIsVisible(true);
		}
	}

	if(!m_CounterBackPopupAnimate.IsReady())
	{
		m_CounterBackPopupAnimate.Tick(dt);
		math::Vec2 Size2 = m_SizeBuyPopup * 1.1f;
		Size2 -= ( m_SizeBuyPopup * 0.1f * m_CounterBackPopupAnimate.GetProgress() );

		math::Vec2 Pos2 = m_PosBuyPopup;
		Pos2 -=	(Size2 * 0.5f);

		if(m_pUIBuyFrame)
		{
			m_pUIBuyFrame->SetSize(Size2);
			m_pUIBuyFrame->SetPosition(Pos2);
		}


	}

	if(m_ConnectinBackButtonDealy > 0)
	{
		m_ConnectinBackButtonDealy -= dt;
		if(m_ConnectinBackButtonDealy <= 0)
		{
			if(m_pConnectBackButton)
				m_pConnectBackButton->SetIsVisible(true);
		}
	}

#ifdef WIN32
	// WINDOW TESTING
	if(input::Keyboard::Instance().IsKeyDown(KEY_C))  // Test Confirm
	{
		OnPurchaseSuccess();
		//RefreshUI();
	}
	else if(input::Keyboard::Instance().IsKeyDown(KEY_B))  // Test Buy
	{
		StartConnectingPopup();
	}

#endif
}




void DELEGATE_CALL		FIGUIRemoveAd::OnButtonBuy(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
#ifndef PRO_VERSION
    
    
    

    //if(InAppPurchaseManager::Instance().CanMakePurchases())
    //{
	//	StartConnectingPopup();
    //    InAppPurchaseManager::Instance().PurchaseProUpgrade(FIGConstants::PRO_UPGRADE);
    //}
#endif
}


void DELEGATE_CALL		FIGUIRemoveAd::OnButtonRestore(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
#ifndef PRO_VERSION

    StartConnectingPopup();
    //InAppPurchaseManager::Instance().RestorePurchase();
#endif
}

void		FIGUIRemoveAd::OnPurchaseSuccess()
{
    FIGUIPauseMenu::Instance().Enable();
    
	// Transactions was successful
	gameflow::UIPopupManager::Instance().EndPopup("ui/connecting.ui");
    ui::UIWidget* pTempWidget = GetWidget("buy_box");
	if(pTempWidget)
		pTempWidget->SetIsVisible(false);
	pTempWidget = GetWidget("frame");
	if(pTempWidget)
		pTempWidget->SetIsVisible(false);
	pTempWidget = GetWidget("buybox_buttons");
	if(pTempWidget)
		pTempWidget->SetIsVisible(false);

#ifndef  PRO_VERSION
	AdManager::Instance().Hide();
#endif

	FIGVars::Instance()->SetInt(FIGVars::VAR_NAMES_IS_FULL_VERSION,1);
	FIGVars::Instance()->Save();
	gameflow::UIPopupManager::Instance().HideAllPopups();
	//RefreshUI();
	this->ForceHide();
	_LOG(MSG_COMMENT, "Yayy!!!! --- he bought our game :) :) :) !!! ");
    
    FIGUIPauseMenu::Instance().RefreshUI();
}

void		FIGUIRemoveAd::OnPurchaseFail()
{
     FIGUIPauseMenu::Instance().Enable();
    
	// Transactions was fail
	gameflow::UIPopupManager::Instance().EndPopup("ui/connecting.ui");
    _LOG(MSG_COMMENT, "Transaction Fail Notification Receive :( ");  
	//RefreshUI();
}

void		FIGUIRemoveAd::StartConnectingPopup()
{
#ifndef PRO_VERSION
    FIGUIPauseMenu::Instance().Disable();
	//InAppPurchaseManager::Instance().SetHandler((InAppPurchaseHandler*) (&(Instance())));
    
	gameflow::UIPopup* pPopup = gameflow::UIPopupManager::Instance().StartPopup("ui/connecting.ui");
	if(pPopup)
	{
		m_pConnectBackButton = pPopup->GetWidget("button_back");
		if(m_pConnectBackButton)
			m_pConnectBackButton->SetIsVisible(false);

		m_ConnectinBackButtonDealy = 15.0f;
	}
#endif
}


void	FIGUIRemoveAd::Show(ui::UIMover::MoverEvent Event)
{
	m_CounterPopupAnimate.Reset();	
	UIPopup::Show(Event);
}

void	FIGUIRemoveAd::Hide(ui::UIMover::MoverEvent Event)
{
	UIPopup::Hide(Event);
}

void	FIGUIRemoveAd::ForceHide()
{
	UIPopup::ForceHide();
}

